package arraylist03;

import java.util.ArrayList;

/*
ArrayList中存储自定义引用类型
   将来这个集合中存储申明元素类型，那么泛型就指定为该类型
 */
public class ArrayListDemo01 {
    public static void main(String[] args) {
        ArrayList<Person> people = new ArrayList<Person>();//我们想用这个people集合存储人，所以泛型定义位Person类型

        Person p1 = new Person("李逍遥", 18);
        Person p2 = new Person("赵灵儿", 17);
        Person p3 = new Person("林月如", 19);

        people.add(p1);//将这三个人添加到集合中
                       //Object[] emementData = new Object[10]
                       //elementData[0] = p1 = new Person("李逍遥", 18);//elementData[0]最终指向李逍遥对象
        people.add(p2);//elementData[1] = p2 = new Person("赵灵儿", 17);
        people.add(p3);//elementData[2] = p3 = new Person("林月如", 19);


        for (Person p : people) {//第一次循环 Person p = p1 = new Person("李逍遥", 18);
                                 //相当于 Perosn p = new Person("李逍遥", 18);
                                 //第二次循环 Person p = p2 = new Person("赵灵儿", 17);
                                 //相当于 Perosn p = new Person("赵灵儿", 17);
            String name = p.getName();
            int age = p.getAge();
            System.out.println(name + " " + age);
        }
    }
}
